home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1997 August / Walnut Creek CDROM.7z / VOL_400 / 466_01 / SRC / ADOUTPUT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-20  |  1.9 KB  |  91 lines

  1. #include <afx.h>
  2. #include "adoutput.h"
  3. #include "errmsg.h"
  4.  
  5. //@doc FILE ADOUTPUT
  6.  
  7.  
  8. //@mfunc Constructor. Initializes the output buffer & count vars.
  9. CAdOutput::CAdOutput()
  10.     m_szOutBuf = NULL;
  11.     m_nBufLen = 0;
  12.     m_nBufCur = 0;
  13. }
  14.  
  15. //@mfunc Destructor. Deletes the output buffer if present.
  16.  
  17. CAdOutput::~CAdOutput()
  18. {
  19.     if(m_szOutBuf)
  20.         delete m_szOutBuf;
  21. }
  22.  
  23. //@mfunc Allocates a memory block for the output buffer and resets
  24. // count variables. 
  25. //@rdesc Zero if successful or errMemory if not enough memory for requested
  26. // buffer.
  27. int CAdOutput::SetOutputBuffer(
  28.     UINT nBufLen)           //@parm How much memory to allocate.
  29. {
  30. TRY
  31. {
  32.     if(m_szOutBuf)
  33.         delete m_szOutBuf;
  34.  
  35.     m_szOutBuf = new char[nBufLen];
  36.     m_nBufLen = nBufLen;
  37.     m_nBufCur = 0;
  38.  
  39.     return 0;
  40. }
  41. CATCH(CMemoryException, e)
  42. {
  43.     return errMemory;
  44. }
  45. END_CATCH
  46. }
  47.  
  48.  
  49. //@mfunc Writes all unwritten text to the output file.
  50.  
  51. void CAdOutput::FlushOutputBuffer(void)
  52. {
  53.     ASSERT(m_szOutBuf);
  54.  
  55.     if(m_nBufCur)
  56.     {
  57.         CFile::Write(m_szOutBuf, m_nBufCur);
  58.         m_nBufCur = 0;
  59.     }
  60. }
  61.  
  62. //@mfunc Writes a chunk of data to the output buffer, flushing the
  63. // buffer first if necessary. Note: Do NOT attempt to write a data
  64. // block larger than the allocated buffer size. If you do so the
  65. // write will be truncated, only the data equal to the buffer size
  66. // will be written, and no error message will occur.
  67.  
  68. void CAdOutput::Write(
  69.     const void* lpBuf,      //@parm Pointer to stuff to write
  70.     UINT nCount)            //@parm How many bytes to write
  71. {
  72.     ASSERT(nCount <= m_nBufLen);
  73.     ASSERT(m_szOutBuf);
  74.  
  75.     if(nCount + m_nBufCur <= m_nBufLen)
  76.     {
  77.         memcpy(m_szOutBuf+m_nBufCur, lpBuf, nCount);
  78.         m_nBufCur += nCount;
  79.     }
  80.     else
  81.     {
  82.         FlushOutputBuffer();
  83.  
  84.         if(nCount > m_nBufLen)
  85.             nCount = m_nBufLen;
  86.  
  87.         Write(lpBuf, nCount);
  88.     }
  89. }
  90.